/**
 * 作者: wgx
 * 日期: 2025-05-14 17:11:56
 * 备注: 玩家大世界行军的战报
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserWorldRecord;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserWorldRecordModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = true;// 是否需要额外的有序列表

    protected long id; // ID
    protected long userId; // 用户ID
    protected int eventId; // 事件id, world_event表的id
    protected long power; // 我方战力
    protected long forces; // 对方战力
    protected String partner = ""; // 带队的干员id
    protected int soldier; // 出发的兵力
    protected int soldierWound; // 伤兵数量
    protected int result; // 结果 (0过期，1失败，2胜利3完胜)
    protected long startTime; // 出征时间
    protected String reward = ""; // 未领取的奖励
    protected int isGetReward; // 是否已经领取奖励1-已领取
    protected String rewardAlread = ""; // 已经领取的奖励(采集事件用)
    protected String param = ""; // 特殊参数 (如搜救事件1-表示当时刚好集齐秘书碎片)
    protected int locationId; // 事件的坑位id
    protected String randomReward = ""; // 随机事件的随机奖励 (客户端显示用，没获得奖励的时候也要显示失败信息)

    public void reset() {
    }

    protected String getKey() {
        return "UserWorldRecord." + id;
    }

    protected String getListKey() {
        return "UserWorldRecord." + userId;
    }

    protected String getListItemKey() {
        return id + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_world_record"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // ID
        obj.put("userId", userId); // 用户ID
        obj.put("eventId", eventId); // 事件id, world_event表的id
        obj.put("power", power); // 我方战力
        obj.put("forces", forces); // 对方战力
        obj.put("partner", partner); // 带队的干员id
        obj.put("soldier", soldier); // 出发的兵力
        obj.put("soldierWound", soldierWound); // 伤兵数量
        obj.put("result", result); // 结果 (0过期，1失败，2胜利3完胜)
        obj.put("startTime", startTime); // 出征时间
        obj.put("reward", reward); // 未领取的奖励
        obj.put("isGetReward", isGetReward); // 是否已经领取奖励1-已领取
        obj.put("rewardAlread", rewardAlread); // 已经领取的奖励(采集事件用)
        obj.put("param", param); // 特殊参数 (如搜救事件1-表示当时刚好集齐秘书碎片)
        obj.put("locationId", locationId); // 事件的坑位id
        obj.put("randomReward", randomReward); // 随机事件的随机奖励 (客户端显示用，没获得奖励的时候也要显示失败信息)
        return obj;
    }

    public PBUserWorldRecord.Builder takeInitBuilder() {
        PBUserWorldRecord.Builder sb = PBUserWorldRecord.newBuilder();
        if (eventId != 0) {
            sb.setEventId(eventId);
        }
        if (power != 0) {
            sb.setPower(power);
        }
        if (forces != 0) {
            sb.setForces(forces);
        }
        if (this.partner != null && this.partner.length() > 0) {
            JsonArray list = new JsonArray(this.partner);
            for(int i=0; i<list.size(); i++) {
                sb.addPartner(list.getInteger(i));
            }
        }
        if (soldier != 0) {
            sb.setSoldier(soldier);
        }
        if (soldierWound != 0) {
            sb.setSoldierWound(soldierWound);
        }
        if (result != 0) {
            sb.setResult(result);
        }
        if (startTime != 0) {
            sb.setStartTime(startTime);
        }
        if (!reward.isEmpty()) {
            sb.setReward(reward);
        }
        if (isGetReward != 0) {
            sb.setIsGetReward(isGetReward);
        }
        if (!rewardAlread.isEmpty()) {
            sb.setRewardAlread(rewardAlread);
        }
        if (!param.isEmpty()) {
            sb.setParam(param);
        }
        if (locationId != 0) {
            sb.setLocationId(locationId);
        }
        if (!randomReward.isEmpty()) {
            sb.setRandomReward(randomReward);
        }
        return sb;
    }

    public PBUserWorldRecord.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserWorldRecord.Builder sb = PBUserWorldRecord.newBuilder();
        if (isFieldUpdate("eventId")) {
            sb.setEventId(eventId);
        }
        if (isFieldUpdate("power")) {
            sb.setPower(power);
        }
        if (isFieldUpdate("forces")) {
            sb.setForces(forces);
        }
        if (isFieldUpdate("partner")) {
            if (this.partner != null && this.partner.length() > 0) {
                JsonArray list = new JsonArray(this.partner);
                for(int i=0; i<list.size(); i++) {
                    sb.addPartner(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearPartner();
                }
            } else {
                sb.clearPartner();
            }
        }
        if (isFieldUpdate("soldier")) {
            sb.setSoldier(soldier);
        }
        if (isFieldUpdate("soldierWound")) {
            sb.setSoldierWound(soldierWound);
        }
        if (isFieldUpdate("result")) {
            sb.setResult(result);
        }
        if (isFieldUpdate("startTime")) {
            sb.setStartTime(startTime);
        }
        if (isFieldUpdate("reward")) {
            sb.setReward(reward);
        }
        if (isFieldUpdate("isGetReward")) {
            sb.setIsGetReward(isGetReward);
        }
        if (isFieldUpdate("rewardAlread")) {
            sb.setRewardAlread(rewardAlread);
        }
        if (isFieldUpdate("param")) {
            sb.setParam(param);
        }
        if (isFieldUpdate("locationId")) {
            sb.setLocationId(locationId);
        }
        if (isFieldUpdate("randomReward")) {
            sb.setRandomReward(randomReward);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getEventId() {
        return eventId;
    }

    public void putEventId(int eventId) {
        if (this.eventId != eventId) {
            this.eventId = eventId;
            updateRedisHashField(getKey(), "eventId", this.eventId);
        }
    }

    public void addEventId(int value) {
        this.eventId += value;
        updateRedisHashField(getKey(), "eventId", this.eventId);
    }

    public long getPower() {
        return power;
    }

    public void putPower(long power) {
        if (this.power != power) {
            this.power = power;
            updateRedisHashField(getKey(), "power", this.power);
        }
    }

    public void addPower(long value) {
        this.power += value;
        updateRedisHashField(getKey(), "power", this.power);
    }

    public long getForces() {
        return forces;
    }

    public void putForces(long forces) {
        if (this.forces != forces) {
            this.forces = forces;
            updateRedisHashField(getKey(), "forces", this.forces);
        }
    }

    public void addForces(long value) {
        this.forces += value;
        updateRedisHashField(getKey(), "forces", this.forces);
    }

    public JsonArray getPartnerArray() {
        if (this.partner != null && this.partner.length() > 0) {
            return new JsonArray(this.partner);
        } else {
            return new JsonArray();
        }
    }

    public String getPartner() {
        return partner;
    }

    public void putPartner(String partner) {
        if (!this.partner.equals(partner)) {
            this.partner = partner;
            updateRedisHashField(getKey(), "partner", this.partner);
        }
    }

    public int getSoldier() {
        return soldier;
    }

    public void putSoldier(int soldier) {
        if (this.soldier != soldier) {
            this.soldier = soldier;
            updateRedisHashField(getKey(), "soldier", this.soldier);
        }
    }

    public void addSoldier(int value) {
        this.soldier += value;
        updateRedisHashField(getKey(), "soldier", this.soldier);
    }

    public int getSoldierWound() {
        return soldierWound;
    }

    public void putSoldierWound(int soldierWound) {
        if (this.soldierWound != soldierWound) {
            this.soldierWound = soldierWound;
            updateRedisHashField(getKey(), "soldierWound", this.soldierWound);
        }
    }

    public void addSoldierWound(int value) {
        this.soldierWound += value;
        updateRedisHashField(getKey(), "soldierWound", this.soldierWound);
    }

    public int getResult() {
        return result;
    }

    public void putResult(int result) {
        if (this.result != result) {
            this.result = result;
            updateRedisHashField(getKey(), "result", this.result);
        }
    }

    public void addResult(int value) {
        this.result += value;
        updateRedisHashField(getKey(), "result", this.result);
    }

    public long getStartTime() {
        return startTime;
    }

    public void putStartTime(long startTime) {
        if (this.startTime != startTime) {
            this.startTime = startTime;
            updateRedisHashField(getKey(), "startTime", this.startTime);
        }
    }

    public void addStartTime(long value) {
        this.startTime += value;
        updateRedisHashField(getKey(), "startTime", this.startTime);
    }

    public String getReward() {
        return reward;
    }

    public void putReward(String reward) {
        if (!this.reward.equals(reward)) {
            this.reward = reward;
            updateRedisHashField(getKey(), "reward", this.reward);
        }
    }

    public int getIsGetReward() {
        return isGetReward;
    }

    public void putIsGetReward(int isGetReward) {
        if (this.isGetReward != isGetReward) {
            this.isGetReward = isGetReward;
            updateRedisHashField(getKey(), "isGetReward", this.isGetReward);
        }
    }

    public void addIsGetReward(int value) {
        this.isGetReward += value;
        updateRedisHashField(getKey(), "isGetReward", this.isGetReward);
    }

    public String getRewardAlread() {
        return rewardAlread;
    }

    public void putRewardAlread(String rewardAlread) {
        if (!this.rewardAlread.equals(rewardAlread)) {
            this.rewardAlread = rewardAlread;
            updateRedisHashField(getKey(), "rewardAlread", this.rewardAlread);
        }
    }

    public String getParam() {
        return param;
    }

    public void putParam(String param) {
        if (!this.param.equals(param)) {
            this.param = param;
            updateRedisHashField(getKey(), "param", this.param);
        }
    }

    public int getLocationId() {
        return locationId;
    }

    public void putLocationId(int locationId) {
        if (this.locationId != locationId) {
            this.locationId = locationId;
            updateRedisHashField(getKey(), "locationId", this.locationId);
        }
    }

    public void addLocationId(int value) {
        this.locationId += value;
        updateRedisHashField(getKey(), "locationId", this.locationId);
    }

    public String getRandomReward() {
        return randomReward;
    }

    public void putRandomReward(String randomReward) {
        if (!this.randomReward.equals(randomReward)) {
            this.randomReward = randomReward;
            updateRedisHashField(getKey(), "randomReward", this.randomReward);
        }
    }

}